#include "Firmware.h"
#include "WavEncoder.h"
#include "Functions.h"
#include "FloatArithmetic.h"
#include <time.h>

void main (int argc, char* argv[])
{
	WavData wavData1 = *getDataFromWav("C:\\Users\\Snuffick\\Desktop\\IPU\\C-Project\\Debug\\test_203_1.wav");
	WavData wavData2 = *getDataFromWav("C:\\Users\\Snuffick\\Desktop\\IPU\\C-Project\\Debug\\test_203_1.wav");
	WavData wavData3 = *getDataFromWav("C:\\Users\\Snuffick\\Desktop\\IPU\\C-Project\\Debug\\test_203_2.wav");

	uint i = 0;
	uint j = 0;
	
	uint time1 = 0;
	uint time2 = 0;

	float percent = 0.0;

	float res1[WINDOW_COUNT*108];
	uint parts1 = 0;

	int res2[WINDOW_COUNT*54];
	sbyte res2_o[WINDOW_COUNT*54];
	uint parts2 = 0;

	float res2_f[WINDOW_COUNT];

	int res3[WINDOW_COUNT*54];
	sbyte res3_o[WINDOW_COUNT*54];
	uint parts3 = 0;

	float res3_f[WINDOW_COUNT];

	printf("Time check example.\n");

	time1 = clock();
	ProcessSignal(wavData1.data.data, wavData1.data.size, res1, &parts1);
	time1 = clock() - time1;

	time2 = clock();
	IProcessSignal(wavData2.data.data, wavData1.data.size, res2, res2_o, &parts2);
	time2 = clock() - time2;

	printf("Standart float time: %f\n\n", time1/(float)CLK_TCK);

	for(i = 0; i < WINDOW_COUNT; i++)
		for(j = i + WINDOW_COUNT; j < parts1*WINDOW_COUNT; j += WINDOW_COUNT)
			res1[i] += res1[j];

	for(i = 0; i < WINDOW_COUNT; i++)
		printf("%d: %f\n", i + 1, res1[i]/(float)WINDOW_COUNT);

	printf("\n");
	printf("My float time: %f\n\n", time2/(float)CLK_TCK);

	for(i = 0; i < WINDOW_COUNT; i++)
		for(j = i + WINDOW_COUNT; j < parts2*WINDOW_COUNT; j += WINDOW_COUNT)
			ISum(res2[i], res2_o[i], res2[j], res2_o[j], &res2[i], &res2_o[i]);
	
	for(i = 0; i < WINDOW_COUNT; i++)
		printf("%d: %f\n", i + 1, res2[i]*powf(10, (float)res2_o[i])/(float)WINDOW_COUNT);

	
	getchar();

	printf("\nCheck coincidence.\n");

	IProcessSignal(wavData3.data.data, wavData3.data.size, res3, res3_o, &parts3);

	for(i = 0; i < WINDOW_COUNT; i++)
		res2_f[i] = res2[i]*powf(10, (float)res2_o[i])/(float)WINDOW_COUNT;

	for(i = 0; i < WINDOW_COUNT; i++)
	{
		for(j = i + WINDOW_COUNT; j < parts3*WINDOW_COUNT; j += WINDOW_COUNT)
			ISum(res3[i], res3_o[i], res3[j], res3_o[j], &res3[i], &res3_o[i]);
	
		res3_f[i] = res3[i]*powf(10, (float)res3_o[i])/(float)WINDOW_COUNT;
	}

	for(i = 0; i < WINDOW_COUNT; i++)
	{
		percent = (res3_f[i] > res2_f[i]) ? fabsf(res3_f[i] - res2_f[i])/res3_f[i] : fabsf(res3_f[i] - res2_f[i])/res2_f[i] ;

		printf("First value: %f	Second value: %f\nMatching percentage: %f%%\n", res2_f[i], res3_f[i], 100 - percent*100);
	}

	getchar();

	//TSin omfg generate lol
	/*
	fopen_s(&fileWav, "C:\\Users\\Darky\\Desktop\\IPU\\C-Project\\Debug\\output2.txt", "w+");
	
	fprintf(fileWav, "const short int TSin[] = { ");
	lol1 = 0;
	lol2 = 16384; 
	for(i = 0; i<= lol2/4; i++)
	{
		lol = (int)(SlowSin(i,lol2)*lol2);
		fprintf(fileWav, "%d,", lol);
		lol1++;
		if(lol1 == 32)
		{
			lol1 = 0;
			fprintf(fileWav, "\n");
		}
	}
	fprintf(fileWav, " }; \n#define SScale %d\n#define SQuarter %d\n#define SThreeQuarter %d\n#define SHalfScale %d\n", lol2, lol2/4, lol2*3/4, lol2/2);

	fclose(fileWav);
	*/	
	//TCos omfg generate lol
	/*
	fopen_s(&fileWav, "C:\\Users\\Darky\\Desktop\\IPU\\C-Project\\Debug\\output2.txt", "w+");
	
	fprintf(fileWav, "const short int TCos[] = { ");
	lol1 = 0;
	lol2 = 16384; 
	for(i = 0; i<= lol2/4; i++)
	{
		lol = (int)(SlowCos(i,lol2)*lol2);
		fprintf(fileWav, "%d,", lol);
		lol1++;
		if(lol1 == 32)
		{
			lol1 = 0;
			fprintf(fileWav, "\n");
		}
	}
	fprintf(fileWav, " }; \n#define CScale %d\n#define CQuarter %d\n#define CThreeQuarter %d\n#define CHalfScale %d\n", lol2, lol2/4, lol2*3/4, lol2/2);

	fclose(fileWav);
	*/
	/*
	k = 40000;
	lol = 0;

	for(i = 0; i<k; i++)
	{
		sin = Sin(i, k)/16384.0;
		slowSin = SlowSin(i, k);
		if(slowSin - sin <= 0.0001)
			lol++;
		else
		{
			printf("slowSin: %f\n Sin: %f\n", slowSin, sin);
		//	getchar();
		}
	}
		
	printf("percent accure of sin: %f\n", lol/(float)k);

	lol = 0;

	for(i = 0; i<k; i++)
	{
		cos = Cos(i, k)/16384.0;
		slowCos = SlowCos(i, k);
		if(slowCos - cos <= 0.001)
			lol++;
	}
	
	printf("percent accure of cos: %f\n", lol/(float)k);
	*/
	//putDataOnWav(&wavData);
}